//获取点击菜单；
var mu="m1";
//搜索条件
var querystr=null;
//
var memberId;



//在线咨询
function tochat(memberid,orderId){
    server.loadindex('page/chat/chat','teacherTopicID='+memberid+(orderId?('&orderId='+orderId):''));
};
//去老师详情页
function todetail(teacherid) {
    if(server.isEmpty(teacherid)){
        pop_up("专家ID为空，不能继续");
        return;
    }
    server.loadindex('page/teacher/teacherdetail/teacherdetail','teachid='+teacherid,true);
};
//评价
function evaluateorder(orderId) {
    // pop_up("功能开发中，请稍后")
    // return;
    server.loadindex('page/order/evaluate/evaluate','orderId='+orderId,true);
}

//去闪测问题答案页面
function toquickawnser(orderId) {
    if(server.isEmpty(orderId)){
        pop_up("获取订单ID失败，请联系管理员");
        return;
    }
    server.loadindex('page/quickask/memberhistoryQuickAsk/memberhistoryQuickAsk','orderId='+orderId,true);
}
//删除未支付的订单
function deleteorder(orderId) {
    console.log(orderId);
    if(server.isEmpty(orderId)){
        pop_up('订单号为空');
        return;
    }
    if(confirm("确定要删除订单吗？")){
        server.req('wechat/order/deleteorder?orderId='+orderId,{},function (res) {
            if(res.code==0){
                pop_up("删除订单成功");
                server.loadindex('page/order/orderlist/orderlist');
            }else{
                pop_up(res.msg);
            }

        },null,'DELETE');
    }else{
        pop_up('取消')
    }

}
// $(function () {
    function writeHtml(data){
        var strHtml = "";
        if(data==null || data.length<=0){
            strHtml='<div style="width:100%;height:40px;line-height:40px;"><label style="margin-left:20px;font-size:14px;color:#333333;">没有订单</label></div>'
        }else
        data.forEach(function (value, index) {
            var strGoods = "";
            var goods = value.goods;
            var strOrderType = "";
            var orderimage;
            if (goods)
                goods.forEach(function (item, id) {
                    orderimage= item.images;
                    if(!orderimage){
                        orderimage='img/icons/default.png';
                    }
                    strGoods = strGoods + '<div class="order-list-item-goods">\n' +
                        '                            <div class="order-list-item-goods-image">\n' +
                        '                                <img src="'+orderimage+'" />\n' +
                        '                            </div>\n' +
                        '                            <div class="order-list-item-goods-title">\n' +
                        '                                <label data-item="itemName">' + item.itemName + '</label>\n' +
                        '                            </div>\n' +
                        '                            <div class="order-list-item-goods-price">\n' +
                        '                                <label>¥</label><label style="margin-left:5px;" data-item="discountPrice">' + item.discountPrice + '</label>\n' +
                        '                            </div>\n' +
                        '                        </div>'
                });
            var strStatus="";
            var strPay="";
            var strChat="";
            var strEvaluate="";
            var strAwnser="";
            var strReEvaluate="";
            var strDelete="";

            //生成再次咨询的按钮
            if((value.serviceType==1 || value.serviceType==2 || value.serviceType==3)&&(value.status!=0)){
                strReEvaluate='                    <div class="order-list-item-btns-btn"  onclick="todetail('+value.teacherId+')">\n' +
                    '                        <label>再次咨询</label>\n' +
                    '                    </div>\n' ;
            }

            if(value.status==0){
                strStatus="待付款";
                strPay = '                    <div class="order-list-item-btns-btn" onclick="submitpay(&apos;'+value.orderNo+'&apos;)">\n' +
                    '                        <label>立即支付</label>\n' +
                    '                    </div>\n';
                strDelete='                    <div class="order-list-item-btns-btn" onclick="deleteorder(&apos;'+value.id+'&apos;)">\n' +
                    '                        <label>删除订单</label>\n' +
                    '                    </div>\n';
                //测试聊天临时使用

                // strChat= '                    <div class="order-list-item-btns-btn" onclick="tochat(&apos;'+value.teacherMemberId+'&apos; ,'+value.id+')">\n' +
                //     '                        <label>在线咨询</label>\n' +
                //     '                    </div>\n';
            }

            else if(value.status==1){
                strStatus="服务中";
                if((value.serviceType==1)&&(value.teacherMemberId)){

                    strChat= '                    <div class="order-list-item-btns-btn" onclick="tochat('+value.teacherMemberId+','+value.id+')">\n' +
                        '                        <label>在线咨询</label>\n' +
                        '                    </div>\n';
                }else if(value.serviceType==2){

                    strAwnser= '                    <div class="order-list-item-btns-btn" onclick="toquickawnser('+value.id+')">\n' +
                        '                        <label>闪测答案</label>\n' +
                        '                    </div>\n';
                }

            }

            else if(value.status==2){
                strStatus="待评价";
                strEvaluate='                    <div class="order-list-item-btns-btn" onclick="evaluateorder(&apos;'+value.id+'&apos;)">\n' +
                '                        <label>评价</label>\n' +
                '                    </div>\n';
                if(value.serviceType==2){

                    strAwnser= '                    <div class="order-list-item-btns-btn" onclick="toquickawnser('+value.id+')">\n' +
                        '                        <label>闪测答案</label>\n' +
                        '                    </div>\n';
                }
            }

            else if(value.status==3)
                strStatus="已完成";
                if(value.serviceType==2){

                    strAwnser= '                    <div class="order-list-item-btns-btn" onclick="toquickawnser('+value.id+')">\n' +
                        '                        <label>闪测答案</label>\n' +
                        '                    </div>\n';
                }
            else if(value.status==-1)
                strStatus="已取消";
            else if(value.status==-2)
                strStatus="申请退款";
            else if(value.status==-3)
                strStatus="已退款";


            var teacherN=value.teacherName;
            //翻译订单类型
            if(value.serviceType==1){
                strOrderType="咨询订单";
            }else if(value.serviceType==2){
                strOrderType="闪测订单";

                if(!teacherN)teacherN='等待抢单';
            }else if(value.serviceType==3){
                strOrderType="提问订单";
            }else if(value.serviceType==4){
                strOrderType="充值订单";
                teacherN="余额充值订单"
            }else if(value.serviceType==5){
                strOrderType="会员认证";
                teacherN="会员认证订单"
            }

            var teacherHead= value.teacherHeadimgurl;

            if(!teacherHead)teacherHead='img/search.png';
            // if(!teacherN)teacherN='等待抢单';

            strHtml = strHtml + '<div class="order-list-item">\n' +
                '                <div class="order-list-item-status">\n' +
                '                    <img src="' + teacherHead + '" />\n' +
                '                    <label style="margin-left:3px;font-weight: bold">' + teacherN +'('+strOrderType+ ')</label><label style="float:right;margin-right:10px">' + strStatus+ '</label>\n' +
                '                </div>\n' +
                '                <div class="Corder-list-item-goods{{id}}">\n' +
                '                    <div data-for="goods">\n' +
                strGoods +
                '                    </div>\n' +
                '                </div>\n' +
                '                <div class="order-list-item-btns">\n' +
                strReEvaluate+
                strPay+
                strChat+strAwnser+strEvaluate+strDelete+
                '                </div>\n' +
                '                <div class="order-list-item-time">\n' +
                '                    <label data-item="createDate">'+value.createDate+'</label><label style="margin-left: 10px">NO:</label><label data-item="orderNo">'+value.orderNo+'</label>\n' +
                '                </div>\n' +
                '            </div>';
        });
        $("#Corder-list").html(strHtml);
    };


    function loadData(querystr){
        server.req('wechat/order/list',querystr,function (res) {
            console.log(res);
            // var data2 = [];
            if(res.code==0){
                var data = res.data;

                writeHtml(data);
                // if(data) {
                // server.loadData(".order-list",res);
                // data.forEach(function (item, index) {
                // server.req('wechat/orderitems/list', {'orderId': item.id}, function (res4) {
                //     goods = res4.data;
                //     item.goods = goods;
                //     // server.loadData(".Corder-list-item-goods"+item.id,item);
                // })
                // server.req('wechat/orderbill/list', {'orderId': item.id}, function (res3) {
                //     bills = res3.data;
                //     item.bills = bills;
                // })
                // });
                // data2.push(item);
                // writeHtml
                // }

            }
            // res.data = data2;

        })
    }
//切换菜单
    function initMenu() {
        $("#m1").css('color','black');
        $("#m1").css('font-weight','normal');
        $("#m2").css('color','black');
        $("#m2").css('font-weight','normal');
        $("#m3").css('color','black');
        $("#m3").css('font-weight','normal');
        $("#m4").css('color','black');
        $("#m4").css('font-weight','normal');
    }
    function menu(e) {
        initMenu();
        $("#"+e).css('color','red');
        $("#"+e).css('font-weight','bold');

        if(e=='m1'){
            loadData(null);
        }else if(e=='m2'){
            querystr={};
            querystr.status=0;
            loadData(querystr);
        }
        else if(e=='m3'){
            querystr={};
            querystr.status=1;
            loadData(querystr);
        }
        else if(e=='m4'){
            querystr={};
            querystr.status=2;
            loadData(querystr);
        }
    };



    // loadData(querystr);
    mu = server.getQueryString("menu");
    if(server.isEmpty(mu)){
        mu="m1";
    }
    menu(mu);


// })


function submitpay(orderNo) {
    console.log("yyy");
    // var e= $("#content_submit").find("input[data-name=orderNo]");
    // console.log(e.val())
    function onBridgeReady(res) {
        console.log(res)
        //var type = $('input:radio[name="paychoose"]:checked').val();
        WeixinJSBridge.invoke('getBrandWCPayRequest', res.data, function(data) {
            console.log(data)
            if (data.err_msg == "get_brand_wcpay_request:ok") {
                server.loadindex( "pay_success","redirect=page/order/orderlist/orderlist");
            } else {
                server.req("wechat/order/removePay?orderNo="+orderNo);
                server.setCache("errorMsg","下单失败！");
                server.loadindex( "error","redirect=page/order/orderlist/orderlist");
            } // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
        });
    }
    server.req("wechat/order/pay",{orderNo:orderNo},function(data){

        if(data.code!=0) {
            //	 server.req("wechat/order/removePay");
            //server.setCache("errorMsg","下单失败！");
            //	server.loadindex( "error","redirect=qbOrder");
            pop_up(data.msg);
            return ;
        }

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', 	onBridgeReady);
            }
        } else {
            onBridgeReady(data);
        }

    },()=>{},"POST","application/x-www-form-urlencoded")



}